﻿using Serilog;
using System.Diagnostics;
using System.Text;

namespace HLStock.Common
{
    public static class MyLogger
    {
        static Serilog.Core.Logger infoLogger;
        static Serilog.Core.Logger sqlLogger;
        static Serilog.Core.Logger errLogger;
        static Serilog.Core.Logger authLogger;
        static MyLogger()
        {
            infoLogger = new LoggerConfiguration()
                .WriteTo.File(RuntimeVariable.ROOT_PATH + "/log/info.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
            sqlLogger = new LoggerConfiguration()
                .WriteTo.File(RuntimeVariable.ROOT_PATH + "/log/sql.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
            errLogger = new LoggerConfiguration()
                .WriteTo.File(RuntimeVariable.ROOT_PATH + "/log/err.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
            authLogger = new LoggerConfiguration()
                .WriteTo.File(RuntimeVariable.ROOT_PATH + "/log/auth.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }

        public static void Auth(string msg, params object[] arg)
        {
            var stacktrace = new StackTrace();
            StringBuilder builder = new StringBuilder();
            for (var i = 1; i < stacktrace?.FrameCount; i++)
            {
                var stack = stacktrace?.GetFrame(i)?.GetMethod();
                string fullPath = String.Format("{0}.{1}", stack?.ReflectedType?.FullName, stack?.Name);
                builder.AppendLine(fullPath);
            }
            builder.AppendFormat(msg, arg);
            authLogger.Information(builder.ToString());
        }
        public static void Log(string msg, params object[] arg)
        {
            var stacktrace = new StackTrace();
            var stack = stacktrace?.GetFrame(1)?.GetMethod();
            string fullPath = String.Format("<{0}.{1}>", stack?.ReflectedType?.FullName, stack?.Name);
            infoLogger.Information(fullPath + String.Format(msg, arg));
        }
        public static void Err(string msg, params object[] arg)
        {
            var stacktrace = new StackTrace();
            StringBuilder builder = new StringBuilder();
            for (var i = 1; i < stacktrace?.FrameCount; i++)
            {
                var stack = stacktrace?.GetFrame(i)?.GetMethod();
                string fullPath = String.Format("{0}.{1}", stack?.ReflectedType?.FullName, stack?.Name);
                builder.AppendLine(fullPath);
            }
            builder.AppendFormat(msg, arg);
            errLogger.Error(builder.ToString());
        }
        public static void Sql(string msg, params object[] arg)
        {
            sqlLogger.Information(String.Format(msg, arg));
        }
    }
}
